<?php
/* -------------------------------------------------------------------------------------
* 	ID:						$Id: order_send.php 11 2013-09-21 11:43:11Z phone.mueller@googlemail.com $
* 	Letzter Stand:			$Revision: 11 $
* 	zuletzt geaendert von:	$Author: siekiera $
* 	Datum:					$Date: 2013-09-21 11:43:11 +0000 (Sat, 21 Sep 2013) $
*
* 	SEO:mercari by Siekiera Media
* 	http://www.seo-mercari.de
*
* 	Copyright (c) since 2011 SEO:mercari
* --------------------------------------------------------------------------------------
* 	based on:
* 	(c) 2000-2001 The Exchange Project  (earlier name of osCommerce)
* 	(c) 2002-2003 osCommerce - www.oscommerce.com
* 	(c) 2003     nextcommerce - www.nextcommerce.org
* 	(c) 2005     xt:Commerce - www.xt-commerce.com
*
* 	Released under the GNU General Public License
* ----------------------------------------------------------------------------------- */

$send_to_customer = 0;
$send_to_admin = 0;

if (isset($_GET['stc']))
	$send_to_customer = $_GET['stc'];
if (isset($_GET['sta']))
	$send_to_admin = $_GET['sta'];

$oID = $_GET['oID'];
$order = new order($oID);
require_once (DIR_FS_CATALOG.DIR_WS_CLASSES.'class.price.php');
$price = new price($order->info['currency'], $order->info['status'], $db);

$smarty->assign('address_label_customer', address_format($order->customer['format_id'], $order->customer, 1, '', '<br />'));
$smarty->assign('address_label_shipping', address_format($order->delivery['format_id'], $order->delivery, 1, '', '<br />'));
$smarty->assign('address_label_payment', address_format($order->billing['format_id'], $order->billing, 1, '', '<br />'));

$smarty->assign('csID', $order->customer['csID']);

$order_data_values = $db->db_query("SELECT
				        				products_id,
				        				orders_products_id,
				        				products_model,
				        				products_name,
				        				products_image,
				        				final_price,
				        				products_quantity
				        			FROM
				        				".TABLE_ORDERS_PRODUCTS."
				        			WHERE
				        				orders_id = ".$db->db_prepare($oID));
$order_data = array ();
while (!$order_data_values->EOF) {
	$attributes_data_values = $db->db_query("SELECT
						        				products_options,
						        				products_options_values,
						        				price_prefix,
						        				options_values_price
						        			FROM
						        				".TABLE_ORDERS_PRODUCTS_ATTRIBUTES."
						        			WHERE
						        				orders_products_id = '".$order_data_values->fields['orders_products_id']."'");

	$getTaxClass = $db->db_query("SELECT products_tax_class_id FROM ".TABLE_PRODUCTS." WHERE products_id = '".$order_data_values->fields['products_id']."' ");

	$attributes_data = '';
	$attributes_model = '';
	$attributes_single_price = '';
	$attributes_final_price = '';
	$attributes_qty = '';
	while (!$attributes_data_values->EOF) {
		$attributes_data .= $attributes_data_values->fields['products_options'].': '.$attributes_data_values->fields['products_options_values'].'<br />';
		$attributes_model .= get_attributes_model($order_data_values->fields['products_id'], $attributes_data_values->fields['products_options_values']);

		$attributes_single_price .= $price->format($attributes_data_values->fields['options_values_price'], true, $getTaxClass->fields['products_tax_class_id']).'<br />';
		$attributes_final_price .= $price->format($attributes_data_values->fields['options_values_price'] * $order_data_values->fields['products_quantity'], true, $getTaxClass->fields['products_tax_class_id']).'<br />';
		$attributes_qty .= ($order_data_values->fields['products_options'] !='Downloads' ? $order_data_values->fields['products_quantity'].'x' : '').'<br />';
		$attributes_data_values->MoveNext();
	}
	$order_data[] = array ('PRODUCTS_MODEL' => $order_data_values->fields['products_model'],
							'PRODUCTS_NAME' => $order_data_values->fields['products_name'],
							'PRODUCTS_IMAGE' => HTTP_SERVER.DIR_WS_CATALOG.$order_data_values->fields['products_image'],
							'PRODUCTS_ATTRIBUTES' => $attributes_data,
							'PRODUCTS_ATTRIBUTES_MODEL' => $attributes_model,
							'PRODUCTS_ATTRIBUTES_SINGLE_PRICE' => $attributes_data_values->fields['price_prefix'].$attributes_single_price,
							'PRODUCTS_ATTRIBUTES_FINAL_PRICE' => $attributes_data_values->fields['price_prefix'].$attributes_final_price,
							'PRODUCTS_ATTRIBUTES_QTY' => $attributes_qty,
							'PRODUCTS_PRICE' => $price->format($order_data_values->fields['final_price'], true),
							'PRODUCTS_SINGLE_PRICE' => $price->format($order_data_values->fields['final_price']/$order_data_values->fields['products_quantity'], true),
							'PRODUCTS_QTY' => $order_data_values->fields['products_quantity']);
	$order_data_values->MoveNext();
}

$oder_total_values = $db->db_query("SELECT
										title,
										text,
										sort_order
									FROM 
										".TABLE_ORDERS_TOTAL."
									WHERE 
										orders_id = ".$db->db_prepare($oID)."
									ORDER BY 
										sort_order ASC");

$order_total = array ();

while (!$oder_total_values->EOF) {
	$order_total[] = array ('TITLE' => $oder_total_values->fields['title'], 'TEXT' => $oder_total_values->fields['text']);
	$oder_total_values->MoveNext();
}

$smarty->assign('oID', $oID);
if ($order->info['payment_method'] != '' && $order->info['payment_method'] != 'no_payment') {
	include (''.DIR_FS_LANGUAGES.$_SESSION['language'].'/modules/payment/'.$order->info['payment_method'].'.php');
	$payment_method = constant(strtoupper('MODULE_PAYMENT_'.$order->info['payment_method'].'_TEXT_TITLE'));
}

$customers_gender = $db->db_query("SELECT customers_gender FROM ".TABLE_CUSTOMERS." WHERE customers_id = '".$order->customer['ID']."' ");
$widerruf = $db->db_query("SELECT content_heading, content_text, content_file FROM ".TABLE_CONTENT_MANAGER." WHERE content_group = '10' AND languages_id = '".(int)$_SESSION['languages_id']."' ");

$smarty->assign('WIDERRUF_HEAD', $widerruf->fields['content_heading']);
if($widerruf->fields['content_file'] !='') {
	ob_start();
	include (DIR_FS_CATALOG.'media/content/'.$widerruf->fields['content_file']);
	$text = stripslashes(ob_get_contents());
	ob_end_clean();
	$smarty->assign('WIDERRUF_TEXT', $text);

} else
	$smarty->assign('WIDERRUF_TEXT', $widerruf->fields['content_text']);
		
$smarty->assign('PAYMENT_METHOD', $payment_method);
$smarty->assign('DATE', utf8_encode(date_long($order->info['date_purchased'])));
$smarty->assign('order_data', $order_data);
$smarty->assign('order_total', $order_total);
$smarty->assign('GENDER', $customers_gender->fields['customers_gender']);
$smarty->assign('VNAME', $order->customer['firstname']);
$smarty->assign('NNAME', $order->customer['lastname']);
$smarty->assign('COMMENTS', $order->info['comments']);
$smarty->assign('EMAIL', $order->customer['email_address']);
$smarty->assign('PHONE',$order->customer['telephone']);

if ($order->info['payment_method'] == 'eustandardtransfer') {
	$smarty->assign('PAYMENT_INFO_HTML', MODULE_PAYMENT_EUTRANSFER_TEXT_DESCRIPTION);
	$smarty->assign('PAYMENT_INFO_TXT', str_replace("<br />", "\n", MODULE_PAYMENT_EUTRANSFER_TEXT_DESCRIPTION));
}

if ($order->info['payment_method'] == 'moneyorder') {
	$smarty->assign('PAYMENT_INFO_HTML', MODULE_PAYMENT_MONEYORDER_TEXT_DESCRIPTION);
	$smarty->assign('PAYMENT_INFO_TXT', str_replace("<br />", "\n", MODULE_PAYMENT_MONEYORDER_TEXT_DESCRIPTION));
}

if ($order->info['payment_method'] == 'cash') {
	$smarty->assign('PAYMENT_INFO_HTML', MODULE_PAYMENT_CASH_TEXT_INFO);
	$smarty->assign('PAYMENT_INFO_TXT', str_replace("<br />", "\n", MODULE_PAYMENT_CASH_TEXT_INFO));
}
require_once (DIR_FS_INC.'inc.get_url_friendly_text.php');
$smarty->caching = false;
require_once (DIR_FS_INC.'inc.get_mail_body.php');
$html_mail = $smarty->fetch('html:order_mail');
$html_mail .= $signatur_html;
$txt_mail = $smarty->fetch('txt:order_mail');
$txt_mail .= $signatur_text;
require_once (DIR_FS_INC.'inc.get_mail_data.php');
$mail_data = get_mail_data('order_mail');

$order_subject = str_replace('{$nr}', $oID, $mail_data['EMAIL_SUBJECT']);
$order_subject = str_replace('{$date}', strftime(DATE_FORMAT_LONG), $order_subject);
$order_subject = str_replace('{$lastname}', $order->customer['lastname'], $order_subject);
$order_subject = str_replace('{$firstname}', $order->customer['firstname'], $order_subject);

if ($send_to_admin==1) {
	php_mail($order->customer['email_address'],
				$order->customer['firstname'].' '.$order->customer['lastname'],
				$mail_data['EMAIL_ADDRESS'],
				$mail_data['EMAIL_ADDRESS_NAME'],
				$mail_data['EMAIL_FORWARD'],
				$order->customer['email_address'],
				$order->customer['firstname'].' '.$order->customer['lastname'],
				'',
				'',
				$order_subject,
				$html_mail,
				$txt_mail);
}

if ($send_to_customer==1) {
	php_mail($mail_data['EMAIL_ADDRESS'],
				$mail_data['EMAIL_ADDRESS_NAME'],
				$order->customer['email_address'],
				$order->customer['firstname'].' '.$order->customer['lastname'],
				'',
				$mail_data['EMAIL_REPLAY_ADDRESS'],
				$from_name,
				'',
				'',
				$order_subject,
				$html_mail,
				$txt_mail,
				true);
}
if (AFTERBUY_ACTIVATED == 'true') {
	require_once (DIR_WS_CLASSES.'class.afterbuy.php');
	$aBUY = new afterbuy_functions($oID);
	if ($aBUY->order_send())
		$aBUY->process_order();
}
$message_stack->add_session(SUCCESS_ORDER_SEND, 'success');
?>